import dayjs from 'dayjs'

// vite 代替require
export function myRequire(path: string) {
  return new URL(`../assets/img/${path}`, import.meta.url).href
}

// 下载excel
export function downloadExcel(file: any, title: string, type?: string) {
  // if (file.result && file.result != 0) {
  //   ElMessage.error('下载失败!')
  //   return false
  // }

  const link = document.createElement('a')
  const blob = new Blob([file], {
    type: type || 'application/vnd.ms-excel'
  })
  link.style.display = 'none'
  //设置连接
  link.href = URL.createObjectURL(blob)
  link.download = `${title}`
  document.body.appendChild(link)
  //模拟点击事件
  link.click()
  document.body.removeChild(link)
}

// 判断响应异常码
export const condition = [
  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
  33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
  62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
  91, 92, 93, 94, 95, 96, 97, 98, 99
]

// 深拷贝 无法处理函数
// export function deepClone<T = any>(obj: T): T {
//   return JSON.parse(JSON.stringify(obj))
// }

// 深拷贝
export function deepClone<T = any>(obj: any): T {
  if (obj === null || obj === undefined || typeof obj !== 'object') {
    return obj
  }

  const newObj: any = Array.isArray(obj) ? [] : ({} as T)

  Object.keys(obj).forEach((key: string) => {
    const value = obj[key]
    newObj[key] = deepClone(value)
  })

  return newObj
}

// 格式化时间 秒重置为零 并将其格式化为时间戳
export const format_allTime = (str: string) => {
  return (dayjs(str).startOf('minute').valueOf() + '').slice(0, -3)

  // return (dayjs(str).valueOf() + '').slice(0, -3)
}

// 格式化运行时间
export const formatSeconds = (seconds: number) => {
  const days = Math.floor(seconds / (3600 * 24))
  const hours = Math.floor((seconds % (3600 * 24)) / 3600)
  const minutes = Math.floor((seconds % 3600) / 60)
  const remainingSeconds = seconds % 60

  return `${days} 天 ${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`
}
